package com.gkd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gkd.constant.MessageConstant;
import com.gkd.exception.AlreadyExistsException;
import com.gkd.exception.DeletionNotAllowedException;
import com.gkd.mapper.PostMapper;
import com.gkd.pojo.dto.CategoryDTO;
import com.gkd.pojo.dto.CategoryPageQueryDTO;
import com.gkd.pojo.entity.Category;
import com.gkd.pojo.entity.Post;
import com.gkd.result.PageResult;
import com.gkd.service.CategoryService;
import com.gkd.mapper.CategoryMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author ZJF
* @description 针对表【category】的数据库操作Service实现
* @createDate 2024-06-15 18:17:38
*/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
    implements CategoryService{

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private PostMapper postMapper;

    /**
     * 新增类别
     * @param categoryDTO
     */
    @Override
    public void add(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        categoryMapper.insert(category);
    }

    /**
     * 类别分页查询
     * @param categoryPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        //查询条件
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(categoryPageQueryDTO.getCategoryName()!="",Category::getCategoryName,categoryPageQueryDTO.getCategoryName());

        //分页查询
        Page<Category> categoryPage = new Page<>(categoryPageQueryDTO.getCurrent(), categoryPageQueryDTO.getPageSize());
        categoryMapper.selectPage(categoryPage,wrapper);

        return new PageResult(categoryPage.getTotal(),categoryPage.getRecords());
    }

    /**
     * 根据id修改分类
     * @param  categoryDTO
     */
    @Override
    public void update(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        categoryMapper.updateById(category);
    }

    /**
     * 批量删除分类
     * @param categoryIds
     */
    @Override
    public void deleteByIds(List<Integer> categoryIds) {
        //查找是否存在关联
        categoryIds.forEach(categoryId->{
            LambdaQueryWrapper<Post> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Post::getCategoryId,categoryId);
            Long count = postMapper.selectCount(wrapper);
            if (count>0){
                throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_POST);
            }
        });
        //删除分类
        categoryMapper.deleteBatchIds(categoryIds);
    }
}




